import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'

export async function GET(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const hardwareVersion = await db.hardwareVersion.findUnique({
      where: { id: params.id },
      include: {
        project: {
          select: { id: true, name: true, key: true }
        },
        bom: {
          select: { id: true, name: true, version: true },
          include: {
            items: {
              include: {
                supplier: {
                  select: { id: true, name: true, code: true }
                }
              }
            }
          }
        },
        creator: {
          select: { id: true, name: true, username: true }
        },
        tests: {
          include: {
            tester: {
              select: { id: true, name: true, username: true }
            }
          },
          orderBy: { createdAt: 'desc' }
        },
        requirements: {
          include: {
            creator: {
              select: { id: true, name: true, username: true }
            },
            assignee: {
              select: { id: true, name: true, username: true }
            }
          }
        }
      }
    })

    if (!hardwareVersion) {
      return NextResponse.json(
        { error: 'Hardware version not found' },
        { status: 404 }
      )
    }

    return NextResponse.json(hardwareVersion)
  } catch (error) {
    console.error('Error fetching hardware version:', error)
    return NextResponse.json(
      { error: 'Failed to fetch hardware version' },
      { status: 500 }
    )
  }
}

export async function PUT(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const body = await request.json()
    const {
      version,
      name,
      description,
      status,
      schematics,
      pcbFiles,
      bomId,
      cost
    } = body

    const hardwareVersion = await db.hardwareVersion.update({
      where: { id: params.id },
      data: {
        ...(version && { version }),
        ...(name && { name }),
        ...(description !== undefined && { description }),
        ...(status && { status }),
        ...(schematics !== undefined && { schematics }),
        ...(pcbFiles !== undefined && { pcbFiles }),
        ...(bomId !== undefined && { bomId }),
        ...(cost !== undefined && { cost })
      },
      include: {
        project: {
          select: { id: true, name: true, key: true }
        },
        bom: {
          select: { id: true, name: true, version: true }
        },
        creator: {
          select: { id: true, name: true, username: true }
        }
      }
    })

    return NextResponse.json(hardwareVersion)
  } catch (error) {
    console.error('Error updating hardware version:', error)
    return NextResponse.json(
      { error: 'Failed to update hardware version' },
      { status: 500 }
    )
  }
}

export async function DELETE(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    await db.hardwareVersion.delete({
      where: { id: params.id }
    })

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Error deleting hardware version:', error)
    return NextResponse.json(
      { error: 'Failed to delete hardware version' },
      { status: 500 }
    )
  }
}